<?php
class ml_model_dbUserExtInfo extends Lib_datamodel_db {
    function __construct() {
        $db_config = ml_factory::load_standard_conf ( 'db' );
        parent::__construct ( 'user_extinfo', $db_config ['user_extinfo'] );
    }
    
    /**
     * 创建记录
     *
     * @param int $uid
     * @return bool
     */
    public function createRecord($uid) {
        if (! $this->init_db ( $uid ))
            return false;
        
        if (! $this->fetch_count ( '`uid` = ' . $uid ))
            return false;
        
        if ($this->get_data () < 1) {
            $data = array (
                    'uid' => $uid 
            );
            return $this->insert ( $data );
        }
    }
    /**
     * 通过第三方登录获取信息创建记录
     *
     * @param int $uid
     * @return bool
     */
    public function createRecordBy3rd($uid, $gender, $birthday) {
        if (! $this->init_db ( $uid ))
            return false;
        
        if (! $this->fetch_count ( '`uid` = ' . $uid ))
            return false;
        
        if ($this->get_data () < 1) {
            $data = array (
                    'uid' => $uid, 
                    'birthday' => $birthday,
                    'gender' =>$gender
            );
            return $this->insert ( $data );
        }
    }
    /**
     * Enter description here...
     *
     * @param unknown_type $uid
     * @param unknown_type $arrData
     * @return unknown
     */
    public function updateShipping($uid, $arrData) {
        if (! $this->init_db ( $uid ))
            return false;
        
        $arrUpdate = array (
                'shipping_name' => $arrData ['truename'], 
                'shipping_address' => $arrData ['address'], 
                'zip_code' => $arrData ['zip_code'], 
                'mobile_phone' => $arrData ['mobile'], 
                'qq' => $arrData ['qq'], 
                'msn' => $arrData ['msn'] 
        );
        
        return $this->update ( $arrUpdate, '`uid` = ' . $uid );
    }
    public function getShippingByUid($uid) {
        if (! $this->init_db ( $uid, self::DB_SLAVE ))
            return false;
        
        $fields = '`shipping_name`,`shipping_address`,`zip_code`,`mobile_phone`,`qq`,`msn`';
        $sql = 'select ' . $fields . ' from `' . $this->table . '` where `uid` = ' . $uid;
        return $this->fetch_row ( $sql );
    }
    
    public function updatePerson($uid, $arrData) {
        if (! $this->init_db ( $uid ))
            return false;
        
        $arrUpdate = array (
                'gender' => $arrData ['gender'], 
                'birthday' => $arrData ['birthday'], 
                'blood_type' => $arrData ['blood_type'], 
                'province_id' => $arrData ['province_id'], 
                'city_id' => $arrData ['city_id'], 
                'interest_tags' => $arrData ['interest_tags'] 
        );
        
        return $this->update ( $arrUpdate, '`uid` = ' . $uid );
    }
    
    public function getPersonByUid($uid) {
        if (! $this->init_db ( $uid, self::DB_SLAVE ))
            return false;
        
        $fields = '`gender`,`birthday`,`blood_type`,`province_id`,`city_id`,`interest_tags`';
        $sql = 'select ' . $fields . ' from `' . $this->table . '` where `uid` = ' . $uid;
        return $this->fetch_row ( $sql );
    }
}